/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tanks.Controllers;

import tanks.BlockType;
import tanks.Cell;
import tanks.Direction;
import tanks.Game;

/**
 *
 * @author Александр
 */
public class AIController extends Controller
    {
        Game game = Game.Instance();
        int dir;
        public void Control()
        {
            if (findTank()&&!getBlock(dir))
            {
                tank.Fire();
            }
            else
            {
                int rnd = game.rnd.nextInt(101);
                if (rnd < 10 && !getBlock(LeftTurn()))
                {
                    dir = LeftTurn();
                }
                else if (rnd < 20 && !getBlock(RightTurn()))
                {
                    dir = RightTurn();
                }
                else if (getBlock(dir))
                {
                    dir = randomDirection();
                }
                tank.Step(dir);
            }
        }
        public AIController()
        {
            dir = randomDirection();
        }
        int LeftTurn()
        {
                switch (dir)
                {
                    case Direction.North:
                        return Direction.West;
                    case Direction.South:
                        return Direction.East;
                    case Direction.West:
                        return Direction.South;
                    default:
                        return Direction.North;
                }
        }
        int RightTurn()
        {
                switch (dir)
                {
                    case Direction.North:
                        return Direction.East;
                    case Direction.South:
                        return Direction.West;
                    case Direction.West:
                        return Direction.South;
                    default:
                        return Direction.North;
                }
        }
        boolean getBlock(int Dir)
        {
            Cell front;
            switch (Dir)
            {
                case Direction.North:
                    if (tank.Y() == 0)
                    {
                        return true;
                    }
                    front = game.Grid[tank.X()][tank.Y() - 1];
                    break;
                case Direction.South:
                    if (tank.Y() == game.fieldHeight - 1)
                    {
                        return true;
                    }
                    front = game.Grid[tank.X()][tank.Y() + 1];
                    break;
                case Direction.West:
                    if (tank.X() == 0)
                    {
                        return true;
                    }
                    front = game.Grid[tank.X() - 1][tank.Y()];
                    break;
                default:
                    if (tank.X() == game.fieldWidth - 1)
                    {
                        return true;
                    }
                    front = game.Grid[tank.X() + 1][tank.Y()];
                    break;
            }
            return front.Block != BlockType.Empty || front.currTank != null;
        }
        int randomDirection()
        {
                switch (game.rnd.nextInt(4))
                {
                    case 0: return Direction.North;
                    case 1: return Direction.South;
                    case 2: return Direction.West;
                    default: return Direction.East;
                }
        }
        boolean findTank()
        {
                switch (dir)
                {
                    case Direction.North:
                        for (int i = tank.Y(); i >= 0; i--)
                        {
                            if (game.Grid[tank.X()][i].Block == BlockType.Solid)
                            {
                                return false;
                            }
                            if (game.Grid[tank.X()][i].currTank != null&&game.Grid[tank.X()][i].currTank != this.tank)
                            {
                                return true;
                            }
                        }
                        break;
                        case Direction.West:
                        for (int i = tank.X(); i >= 0; i--)
                        {
                            if (game.Grid[i][tank.Y()].Block == BlockType.Solid)
                            {
                                return false;
                            }
                            if (game.Grid[i][tank.Y()].currTank != null && game.Grid[i][tank.Y()].currTank != this.tank)
                            {
                                return true;
                            }
                        }
                        break;
                            case Direction.East:
                        for (int i = tank.X(); i <game.fieldWidth; i++)
                        {
                            if (game.Grid[i][tank.Y()].Block == BlockType.Solid)
                            {
                                return false;
                            }
                            if (game.Grid[i][tank.Y()].currTank != null && game.Grid[i][tank.Y()].currTank != this.tank)
                            {
                                return true;
                            }
                        }
                        break;
                            case Direction.South:
                        for (int i = tank.Y(); i < game.fieldHeight; i++)
                        {
                            if (game.Grid[tank.X()][i].Block == BlockType.Solid)
                            {
                                return false;
                            }
                            if (game.Grid[tank.X()][i].currTank != null && game.Grid[tank.X()][i].currTank != this.tank)
                            {
                                return true;
                            }
                        }
                        break;                        
                }
                return false;
            }
    }
